package com.gxljc.bear.crawler.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class StringUtil {
	private static final Map<Character, Character> SIMPLE_NUMBER_MAP;// 最简单数字映射

	static {
		SIMPLE_NUMBER_MAP = new ConcurrentHashMap<Character, Character>();
		SIMPLE_NUMBER_MAP.put('一', '1');
		SIMPLE_NUMBER_MAP.put('二', '2');
		SIMPLE_NUMBER_MAP.put('三', '3');
		SIMPLE_NUMBER_MAP.put('四', '4');
		SIMPLE_NUMBER_MAP.put('五', '5');
		SIMPLE_NUMBER_MAP.put('六', '6');
		SIMPLE_NUMBER_MAP.put('七', '7');
		SIMPLE_NUMBER_MAP.put('八', '8');
		SIMPLE_NUMBER_MAP.put('九', '9');
		SIMPLE_NUMBER_MAP.put('十', '1');
	}

	public static String toNumber(String string) {
		if (string == null) {
			return null;
		}
		char[] cs = string.toCharArray();
		StringBuffer sb = new StringBuffer();
		for (char fromC : cs) {
			Character toC = SIMPLE_NUMBER_MAP.get(fromC);
			sb.append((toC == null) ? fromC : toC);
		}
		return sb.toString();
	}

	public static double computeSimilarity(String word1, String word2) {
		if (word1 == null || word2 == null) {
			return 0;
		}
		Set<Character> cs1 = new HashSet<Character>(word2Chars(word1));
		Set<Character> cs2 = new HashSet<Character>(word2Chars(word2));
		Set<Character> csUnion = new HashSet<Character>();
		Set<Character> csInters = new HashSet<Character>(cs1);
		csUnion.addAll(cs1);
		csUnion.addAll(cs2);
		csInters.retainAll(cs2);
		return (double) csInters.size() / (double) csUnion.size();
	}

	public static List<Character> word2Chars(String word) {
		List<Character> cs = new ArrayList<Character>();
		for (Character c : word.toCharArray()) {
			cs.add(c);
		}
		return cs;
	}

	/**
	 * 最大公共子串Longest Common String, LCS
	 * 
	 * @param string1
	 * @param string2
	 * @return
	 */
	public static String getLCString(String string1, String string2) {
		char[] str1 = string1.toCharArray();
		char[] str2 = string2.toCharArray();
		int i, j;
		int len1, len2;
		len1 = str1.length;
		len2 = str2.length;
		int maxLen = len1 > len2 ? len1 : len2;
		int[] max = new int[maxLen];
		int[] maxIndex = new int[maxLen];
		int[] c = new int[maxLen]; // 记录对角线上的相等值的个数

		for (i = 0; i < len2; i++) {
			for (j = len1 - 1; j >= 0; j--) {
				if (str2[i] == str1[j]) {
					if ((i == 0) || (j == 0))
						c[j] = 1;
					else
						c[j] = c[j - 1] + 1;
				} else {
					c[j] = 0;
				}

				if (c[j] > max[0]) { // 如果是大于那暂时只有一个是最长的,而且要把后面的清0;
					max[0] = c[j]; // 记录对角线元素的最大值，之后在遍历时用作提取子串的长度
					maxIndex[0] = j; // 记录对角线元素最大值的位置

					for (int k = 1; k < maxLen; k++) {
						max[k] = 0;
						maxIndex[k] = 0;
					}
				} else if (c[j] == max[0]) { // 有多个是相同长度的子串
					for (int k = 1; k < maxLen; k++) {
						if (max[k] == 0) {
							max[k] = c[j];
							maxIndex[k] = j;
							break; // 在后面加一个就要退出循环了
						}

					}
				}
			}
		}
		StringBuffer sb = new StringBuffer();
		for (j = 0; j < maxLen; j++) {
			if (max[j] > 0) {
				for (i = maxIndex[j] - max[j] + 1; i <= maxIndex[j]; i++) {
					sb.append(str1[i]);
				}
			}
		}
		return sb.toString();
	}

}
